package cn.janescott.inner.space.data;

import com.alibaba.dubbo.spring.boot.annotation.EnableDubboConfiguration;
import org.hibernate.boot.model.naming.PhysicalNamingStrategy;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.BeansException;
import org.springframework.beans.factory.config.BeanPostProcessor;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.autoconfigure.jdbc.DataSourceAutoConfiguration;
import org.springframework.context.annotation.Bean;
import org.springframework.data.repository.Repository;

import javax.sql.DataSource;

/**
 * 数据服务启动类
 * 去除数据库自动配置，因为采用自己编写的自动配置类
 *
 * @author scott
 * @date 2018/07/30
 */
@EnableDubboConfiguration
@SpringBootApplication(exclude = {DataSourceAutoConfiguration.class})
public class DataApplication {

    public static void main(String[] args) {
        SpringApplication.run(DataApplication.class, args);
    }

    @Bean
    public BeanPostProcessor getBeanPostProcessor() {
        return new BeanPostProcessor() {
            private Logger logger = LoggerFactory.getLogger(getClass());

            @Override
            public Object postProcessBeforeInitialization(Object bean, String beanName) throws BeansException {
                if (bean instanceof Repository) {
                    logger.info("生成仓库对象：" + beanName + "初始化之前.");
                }

                if (bean instanceof PhysicalNamingStrategy) {
                    logger.info("数据库命名策略");
                }

                if (bean instanceof DataSource) {
                    logger.info("数据源");
                }

                if (beanName.contains("control")) {
                    logger.info(beanName);
                }

                return bean;
            }

            @Override
            public Object postProcessAfterInitialization(Object bean, String beanName) throws BeansException {
                if (bean instanceof Repository) {
                    logger.info("生成仓库对象：" + beanName + "初始化之后.");
                }
                return bean;
            }
        };
    }
}
